//
//  NEDFormSeationView.m
//  NEDFormView_Example
//
//  Created by 000 on 2022/1/7.
//  Copyright © 2022 000. All rights reserved.
//

#import "NEDFormSeationView.h"
#import <NEDApp/NEDApp+Mediator.h>
#import <Masonry/Masonry.h>

@interface NEDFormSeationView()

@property (retain, nonatomic) UIStackView * mainStackView;
@property (retain, nonatomic) UIStackView * centerStackView;

/// 中间一块视图
@property (retain, nonatomic) UIView * contentView;

@property (retain, nonatomic) UIImageView * rightArrowImageView;

@property (retain, nonatomic) UITapGestureRecognizer * tap;

@end

@implementation NEDFormSeationView

-(instancetype)init{
    self = [super init];
    if(self){
        [self form_baseUISetup];
        [self form_SetupUI];
    }
    return self;
}

-(void)setFormItem:(NEDFormItem *)formItem{
    _formItem = formItem;
    
    WS(weakSelf)
    
    NEDFormSectionLayout * layout = formItem.layout;
    
    if(layout.spaceTop){
        self.topView = [UIView new];
        self.topView.backgroundColor = layout.spaceTopColor;
        [self.mainStackView addArrangedSubview:self.topView];
        [self.topView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(layout.spaceTop);
        }];
    }
    
    if(layout.showTopSeparatorLine){
        self.topLineView = [UIView new];
        self.topLineView.backgroundColor = layout.separatorLineColor;
        [self.mainStackView addArrangedSubview:self.topLineView];
        [self.topLineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(layout.separatorLineHeight);
        }];
    }
    
    // center
    {
        [self.mainStackView addArrangedSubview:self.contentView];
        
        if(formItem.leftContent && formItem.leftContent.initViewBlock){
            self.leftView = formItem.leftContent.initViewBlock(formItem,formItem.leftContent);
            [self.centerStackView addArrangedSubview:self.leftView];
            if(layout.widthContentLeft){
                [self.leftView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.width.mas_equalTo(layout.widthContentLeft);
                }];
            }
            
            [formItem.leftContent setGetViewBlock:^UIView * _Nullable{
                return weakSelf.leftView;
            }];
        }
        
        if(formItem.centerContent && formItem.centerContent.initViewBlock){
            self.centerView = formItem.centerContent.initViewBlock(formItem,formItem.centerContent);
            [self.centerStackView addArrangedSubview:self.centerView];
            
            [formItem.centerContent setGetViewBlock:^UIView * _Nullable{
                return weakSelf.centerView;
            }];
        }
        
        if(formItem.rightContent && formItem.rightContent.initViewBlock){
            self.rightView = formItem.rightContent.initViewBlock(formItem,formItem.rightContent);
            [self.centerStackView addArrangedSubview:self.rightView];
            if(layout.widthContentRight){
                [self.rightView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.width.mas_equalTo(layout.widthContentRight);
                }];
            }
            
            [formItem.rightContent setGetViewBlock:^UIView * _Nullable{
                return weakSelf.rightView;
            }];
        }
        
        // 中间的左右间距
        if(layout.marginLeft || layout.marginRight){
            [self.centerStackView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.offset(layout.marginLeft);
                make.right.offset(-layout.marginRight);
            }];
        }
        
        self.contentView.backgroundColor = layout.backgroundColor;
        self.centerStackView.spacing = layout.spaceStack;
    }
    
    if(layout.showBottomSeparatorLine){
        self.bottomLineView = [UIView new];
        self.bottomLineView.backgroundColor = layout.separatorLineColor;
        [self.mainStackView addArrangedSubview:self.bottomLineView];
        [self.bottomLineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(layout.separatorLineHeight);
        }];
    }
    
    if(layout.spaceBottom){
        self.bottomView = [UIView new];
        self.bottomView.backgroundColor = layout.spaceBottomColor;
        [self.mainStackView addArrangedSubview:self.bottomView];
        [self.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(layout.spaceBottom);
        }];
    }
    
    //显示右侧箭头视图
    if(layout.showRightArrowView){
        if(!self.rightArrowImageView){
            self.rightArrowImageView = [UIImageView new];
            self.rightArrowImageView.contentMode = UIViewContentModeScaleAspectFit;
            if(APP.mediator &&
               APP.mediator.helper &&
               [APP.mediator.helper respondsToSelector:@selector(form_arrowImage)]){
                self.rightArrowImageView.image = [APP.mediator.helper form_arrowImage];
            }else if (@available(iOS 13.0, *)) {
                self.rightArrowImageView.image = [UIImage systemImageNamed:@"chevron.right"];
            } else {
                NSLogError(@"未定义右侧箭头显示图片，可以实现APP.mediator.helper.form_arrowImage来设置");
            }
            [self addSubview:self.rightArrowImageView];
            [self.rightArrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self).offset(-10);
                make.centerY.equalTo(self);
            }];
        }
        self.rightArrowImageView.hidden = NO;
    }else{
        self.rightArrowImageView.hidden = YES;
    }
    
//    cell.showAccessoryArrowView = item.layout.showAccessoryArrowView;
//    cell.showBottomLine = item.layout.showBottomLine;
    
    if(self.formItem.clickBlock){
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        self.contentView.userInteractionEnabled = YES;
        [self.contentView addGestureRecognizer:tap];
    }
}


- (void)form_baseUISetup{
    self.mainStackView = [UIStackView new];
    self.mainStackView.backgroundColor = [UIColor clearColor];
    self.mainStackView.axis = UILayoutConstraintAxisVertical;
    [self addSubview:self.mainStackView];
    [self.mainStackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    self.contentView = [UIView new];
    self.contentView.clipsToBounds = NO;
    
    self.centerStackView = [UIStackView new];
    self.centerStackView.backgroundColor = [UIColor clearColor];
    self.centerStackView.axis = UILayoutConstraintAxisHorizontal;
    [self.contentView addSubview:self.centerStackView];
    [self.centerStackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
}

- (void)tapAction:(UITapGestureRecognizer *)gesture{
    if(self.formItem.clickBlock){
        self.formItem.clickBlock(self.sectionIndex, self, self.formItem);
    }
}

#pragma mark - public

- (void)form_SetupUI{
}


@end
